/**
 *麻将游戏场景 
 */
package com.mj.scence.game
{
	import com.gamebase.net.rpc.baseTypeEx.int16;
	import com.gamebase.net.rpc.baseTypeEx.uint16;
	import com.mj.model.CommonData;
	import com.mj.model.MjModel;
	import com.mj.model.ProtocolMap;
	import com.mj.scence.game.view.GameScenceV;
	import com.mj.scence.game.view.MJCard;
	import com.mj.vo.Card;
	import com.mj.vo.RoleData;
	import com.mj.vo.SCMJ2EnterRoomRes;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	public class GameScence
	{
		private var model:MjModel;
		public var view:GameScenceV;
		private var _areaId:uint16;
		private var _isBanker:Boolean;//是否为庄家
		private var takeCardRule:TakeCardRule;
		
		 
		public function GameScence()
		{ 
			super();
			view = new GameScenceV();
			view.addEventListener("startTakeCard", startTakeCard);
			takeCardRule = new TakeCardRule();
			model = MjModel.I;
			model.addListener(ProtocolMap.SC_MJ2_ENTER_ROOM_RES, onEnterArea);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_ENTER_ROOM, otherJoin);
			model.addListener(ProtocolMap.SC_MJ2_READY_RES, selfReady);
			model.addListener(ProtocolMap.SC_MJ2_NOTIFY_READY, otherReady);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_BANKER, onBanker);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_START_POS, onStartPos);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_INIT_CARD, onGetCardInfo);
			model.addListener(ProtocolMap.SC_MJ2_PUT_CARD_RES, onTakeCard);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_GET_CARD, onOtherTakeCard);
			model.addListener(ProtocolMap.SC_NOTIFY_MJ2_PUT_CARD, onOtherPutCard);
			
			
		}
		
		public function enterArea(areaId:int):void
		{
			if(_areaId == null)
			{
				_areaId = new uint16(areaId);
			}
			else
				_areaId.value = areaId;
			model.enterArea(_areaId);
		}
		
		private function onEnterArea($data:SCMJ2EnterRoomRes):void
		{
			if($data == null)//没有对方玩家
				view.iniOtherUserPanel(null, false);
			else
				view.iniOtherUserPanel($data.other, Boolean($data.ready.value));
			view.iniSelfUserPanel(CommonData.selfInfo);
				
		}
		
		private function otherJoin($data:RoleData):void
		{
			view.iniOtherUserPanel($data, false);
		}
		
		private function selfReady():void
		{
			view.selfReady();
		}
		
		private function otherReady():void
		{
			view.otherReady();
		}
		
		//定庄家
		private function onBanker(points:Vector.<int16>):void
		{
			
			if(points[0].value >= points[1].value)
			{
				_isBanker = true;
			}
			else
			{
				_isBanker = false;
			}
			view.onBanker(points[0].value, points[1].value, _isBanker);
			
			layoutCoverCard();
		}
		
		//重新洗牌
		private function layoutCoverCard():void
		{
			var cards:Vector.<MJCard> = view.layoutCoverCards();
			takeCardRule.ini(cards, _isBanker);
		}
		
		//定起牌位置
		private function onStartPos(points:Vector.<int16>):void
		{
			
			view.onStartPos(points[0].value, points[1].value);
			takeCardRule.iniStartPos(points[0].value + points[1].value);
		}
		
		//获取到牌信息
		private function onGetCardInfo(cards:Vector.<Card>):void
		{
			takeCardRule.iniMyCardsInfo(cards);
			view.onGetCardInfo(cards);
		}
		
		private function onTakeCard($card:Card):void
		{
			view.takeOneCard(takeCardRule.takeOneCard(), $card.id.value, true);
		}
		
		private function onOtherTakeCard($card:Card):void
		{
			view.takeOneCard(takeCardRule.takeOneCard(), $card.id.value, false);
		}
		
		private function onOtherPutCard($card:Card):void
		{
			view.onOtherPutCard($card.id.value);
		}
		
		//开始起牌
		private function startTakeCard(evt:Event=null):void
		{
			var pareCards:Vector.<MJCard>;
			var takeCardHandle:uint;
			takePareCards();
			
			function takePareCards():void
			{
				pareCards = takeCardRule.takeIniCard();
				
				if(pareCards)
				{
					view.takePareCard(pareCards[0], pareCards[1]);
					takeCardHandle = flash.utils.setTimeout(takePareCards, 1000);
				}
				else
				{
					flash.utils.clearTimeout(takeCardHandle);
					gameBegin();
				}
			}
		}
		
		//洗好牌后游戏开始
		private function gameBegin():void
		{
			view.gameBegin();
			model.takeCard();
		}
		
		//游戏结束
		public function gameEnd():void
		{
			view.gameEnd();
		}
		
	}
}